//
// Created by 黄扬航 on 2023/10/1.
//

#ifndef SERVER_CLIENT_H
#define SERVER_CLIENT_H
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <winsock2.h>
#include <algorithm>
#include <unistd.h>
#include<queue>
#include <ctime>
#include <ws2tcpip.h>
#include "CheckType.h"
#include <functional>

#pragma comment(lib, "ws2_32.lib")

struct message
{
    message(std::string Message_in,int MessageFrom_in,int MessageTo_in):
            Message(Message_in),
            MessageFrom(MessageFrom_in),
            MessageTo(MessageTo_in)
            {Time = time(NULL);};

    std::string Message;
    int SquenceId;   //服务端决定，递增。
    int MessageFrom;
    int MessageTo;
    bool IsSafe = false;
    time_t Time;
};
class Client {
private:
    SOCKET ClientSocket;
    sockaddr_in ClientAddr;
    std::string ServerIP = "192.168.137.1";
    sockaddr_in ServerAddr;
    int Recevtimeout = 3000; //3s
    struct timeval timeout{3,0};
    int ID;
    int CurrentMessageID = 0;
public:
    Client();
    int ConnectToServer(char* Ip,int port);
    void SendMesage(struct message Message);
    int CheckSendACK(SOCKET TempCLient,int message_length,int ACK_Type);
    SOCKET ShortConnection();
    int SetTimeOut(SOCKET &socket);
    void ReceiveMessage();
};
#endif //SERVER_CLIENT_H
